/**
 * 
 */
package fr.mynounou.web.model.dao;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;

import fr.mynounou.entities.Nounou;
import fr.mynounou.entities.contrats.Contrat;

/**
 * @author btavernier
 * 
 */
@SuppressWarnings("serial")
@ManagedBean(name = "ContratDAO")
@ApplicationScoped
public class ContratDaoImpl extends JpaDAO implements ContratDao, Serializable {
	private static final String JPA_UNIT_NAME = "MyNounou";

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.mynounou.web.model.dao.ContratDao#listContrats()
	 */
	@Override
	public List<Contrat> listContrats() {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.mynounou.web.model.dao.ContratDao#listContrats(fr.mynounou.entities
	 * .Nounou)
	 */
	@Override
	public List<Contrat> listContrats(Nounou n) {

		TypedQuery<Contrat> q = getEntityManager().createNamedQuery(
				"SELECT c FROM Contrat c WHERE c.nounou.id = :id",
				Contrat.class);
		q.setParameter("id", n.getId());
		
		return q.getResultList();

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.mynounou.web.model.dao.ContratDao#createContrat(fr.mynounou.entities
	 * .contrats.Contrat)
	 */
	@Override
	public void createContrat(Contrat c) {
		try {
			getEntityManager().getTransaction().begin();
			getEntityManager().persist(c);
			getEntityManager().getTransaction().commit();
		} catch (Exception e) {
			getEntityManager().getTransaction().rollback();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.mynounou.web.model.dao.ContratDao#destroyContrat(fr.mynounou.entities
	 * .contrats.Contrat)
	 */
	@Override
	public void destroyContrat(Contrat c) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.mynounou.web.model.dao.ContratDao#editContrat(fr.mynounou.entities
	 * .contrats.Contrat)
	 */
	@Override
	public void editContrat(Contrat c) {
		// TODO Auto-generated method stub

	}

	@Override
	public EntityManager getEntityManager() {
		if (em == null) {
			em = Persistence.createEntityManagerFactory(JPA_UNIT_NAME)
					.createEntityManager();
		}
		return em;
	}

}
